import time
import random
import sys
import os

class Colors:
    """颜色代码类"""
    RED = '\033[91m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    BLUE = '\033[94m'
    MAGENTA = '\033[95m'
    CYAN = '\033[96m'
    WHITE = '\033[97m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    BLINK = '\033[5m'
    RESET = '\033[0m'

# 颜色组合
COLOR_COMBOS = [
    [Colors.RED, Colors.BOLD],
    [Colors.GREEN, Colors.BOLD],
    [Colors.YELLOW, Colors.BOLD],
    [Colors.BLUE, Colors.BOLD],
    [Colors.MAGENTA, Colors.BOLD],
    [Colors.CYAN, Colors.BOLD, Colors.UNDERLINE],
    [Colors.WHITE, Colors.BOLD, Colors.BLINK]
]

def clear_screen():
    """清屏函数"""
    os.system('cls' if os.name == 'nt' else 'clear')

def get_random_color_combo():
    """获取随机颜色组合"""
    return random.choice(COLOR_COMBOS)

def advanced_typewriter(text, delay=0.03, effect_type="normal"):
    """
    高级打字机效果
    
    参数:
    text: 文本内容
    delay: 延迟时间
    effect_type: 效果类型（normal, wave, random, gradient）
    """
    if effect_type == "normal":
        # 普通炫彩效果
        for i, char in enumerate(text):
            color_combo = get_random_color_combo()
            color_code = ''.join(color_combo)
            print(f"{color_code}{char}{Colors.RESET}", end='', flush=True)
            time.sleep(delay)
    
    elif effect_type == "wave":
        # 波浪颜色效果
        colors = [Colors.RED, Colors.YELLOW, Colors.GREEN, Colors.CYAN, Colors.BLUE, Colors.MAGENTA]
        for i, char in enumerate(text):
            color_index = (i // 2) % len(colors)
            print(f"{Colors.BOLD}{colors[color_index]}{char}{Colors.RESET}", end='', flush=True)
            time.sleep(delay)
    
    elif effect_type == "random":
        # 完全随机效果
        for char in text:
            color_combo = get_random_color_combo()
            # 随机决定是否加粗、下划线等
            if random.random() > 0.7:
                color_combo.append(Colors.UNDERLINE)
            color_code = ''.join(color_combo)
            print(f"{color_code}{char}{Colors.RESET}", end='', flush=True)
            time.sleep(delay * random.uniform(0.5, 2.0))  # 随机速度

def display_banner(cycle_count):
    """显示漂亮的横幅"""
    banner_width = 70
    banner = [
        "✨" + "=" * (banner_width - 2) + "✨",
        f"🎯 循环播放次数: {cycle_count} 🎯",
        "🌟 Python打字机炫彩效果 🌟",
        "✨" + "=" * (banner_width - 2) + "✨"
    ]
    
    for line in banner:
        print(f"{Colors.CYAN}{line.center(banner_width)}{Colors.RESET}")

def main_enhanced():
    """增强版主函数"""
    message = "两年翻身人上人❤❤❤百万年薪不是梦❤❤❤我爱北京雁栖湖应用数学研究院❤❤❤"
    effects = ["normal", "wave", "random"]
    effect_names = ["普通炫彩", "波浪效果", "随机特效"]
    
    cycle_count = 0
    
    try:
        while True:
            clear_screen()
            cycle_count += 1
            
            # 显示横幅
            display_banner(cycle_count)
            print()
            
            # 选择效果类型（循环切换）
            effect_index = (cycle_count - 1) % len(effects)
            current_effect = effects[effect_index]
            
            print(f"{Colors.YELLOW}当前效果: {effect_names[effect_index]}{Colors.RESET}")
            print()
            
            # 显示打字机效果
            advanced_typewriter(message, delay=0.03, effect_type=current_effect)
            
            print("\n")
            print(f"{Colors.GREEN}提示: 按 Ctrl+C 退出程序{Colors.RESET}")
            print(f"{Colors.MAGENTA}下一个效果: {effect_names[(effect_index + 1) % len(effects)]}{Colors.RESET}")
            
            # 暂停时间根据效果类型变化
            pause_time = 3 if current_effect == "random" else 2
            time.sleep(pause_time)
            
    except KeyboardInterrupt:
        print(f"\n\n{Colors.RED}❤️  {Colors.RESET}{Colors.YELLOW}感谢使用！再见！{Colors.RESET}{Colors.RED}  ❤️{Colors.RESET}\n")

if __name__ == "__main__":
    # Windows系统颜色支持
    if os.name == 'nt':
        os.system('')
    
    print("请选择模式:")
    print("1. 基础炫彩效果")
    print("2. 增强特效版本")
    
    choice = input("请输入选择 (1 或 2, 默认为1): ").strip()
    
    if choice == "2":
        main_enhanced()
    else:
        # 运行基础版本
        import first_version
        first_version.main()